﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Yunzuan.Foundation.Entities
{
    /// <summary>
    /// 用于调用方法
    /// </summary>
    [Serializable]
    public class HashParam : Dictionary<string, object>
    {
        /// <summary>
        /// 获取自动反序列化实体
        /// </summary>
        public T GetEntity<T>() where T : BaseEntity, new()
        {
            return this.Get<T>("Entity");
        }

        /// <summary>
        /// 获取自动反序列化实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="keyName"></param>
        /// <returns></returns>
        public T Get<T>(string keyName) where T : BaseEntity, new()
        {
            if (!this.ContainsKey(keyName) || this[keyName] == null)
            {
                return default(T);
            }

            T result = Yunzuan.Foundation.Serializer.Json.Deserialize<T>(this[keyName].ToString());
            return result;
        }

        public int? GetInt(string columnName)
        {
            if (!this.ContainsKey(columnName) || this[columnName] == null || this[columnName].ToString() == "")
            {
                return null;
            }
            return Convert.ToInt32(this[columnName]);
        }

        public short? GetShort(string columnName)
        {
            if (!this.ContainsKey(columnName) || this[columnName] == null || this[columnName].ToString() == "")
            {
                return null;
            }
            return Convert.ToInt16(this[columnName]);
        }

        public int GetInt(string columnName, int defaultValue)
        {
            if (!this.ContainsKey(columnName) || this[columnName] == null || this[columnName].ToString() == "")
            {
                return defaultValue;
            }
            return Convert.ToInt32(this[columnName]);
        }

        public string GetString(string columnName)
        {
            if (this.ContainsKey(columnName) && this[columnName] != null)
            {
                return this[columnName].ToString();
            }
            return string.Empty;
        }

        public decimal? GetDecimal(string columnName)
        {
            if (this[columnName] == null || this[columnName].ToString() == "")
            {
                return null;
            }
            return Convert.ToDecimal(this[columnName]);
        }

        public decimal GetDecimal(string columnName, decimal defaultValue)
        {
            if (this[columnName] == null || this[columnName].ToString() == "")
            {
                return defaultValue;
            }
            return Convert.ToDecimal(this[columnName]);
        }

        public DateTime? GetDataTime(string columnName)
        {
            if (this[columnName] == null || this[columnName].ToString() == "")
            {
                return null;
            }
            return Convert.ToDateTime(this[columnName]);
        }

        public DateTime GetDataTime(string columnName, DateTime defaultValue)
        {
            if (this[columnName] == null || this[columnName].ToString() == "")
            {
                return defaultValue;
            }
            return Convert.ToDateTime(this[columnName]);
        }

        public bool GetBool(string columnName)
        {
            if (this[columnName] == null || this[columnName].ToString() == "")
            {
                return false;
            }
            return Convert.ToBoolean(this[columnName]);
        }

        //public int AddressID { get; set; }
    }

    ///// <summary>
    ///// 用于调用方法
    ///// </summary>
    //[Serializable]
    //public class HashParam<T> : Dictionary<string, object> 
    //    where T : class, new()
    //{
    //    public T Entity { get; set; }
    //}

    ///// <summary>
    ///// 用于调用方法
    ///// </summary>
    //[Serializable]
    //public class HashParam<T1, T2> : Dictionary<string, object>
    //    where T1 : class, new()
    //    where T2 : class,new()
    //{
    //    public T1 Entity { get; set; }
    //    public T2 EntitySecond { get; set; }
    //}
}
